package com.cbg.score.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbg.base.utils.Result;
import com.cbg.score.entity.ScChanal;
import com.cbg.score.entity.ScScore;
import com.cbg.score.service.IScChanalService;
import com.cbg.score.service.IScScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cbg.base.core.SuperController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangkexiong
 * @since 2018-12-22
 */
@RestController
@RequestMapping("/score")
public class ScScoreController extends SuperController<IScScoreService, ScScore> {
    /***
     * 根据乘车人id进行查询
     */
    @Autowired
    private IScScoreService scScoreService;
    @Autowired
    private IScChanalService scChanalService;
    @GetMapping("/{riderId}")
    public Result<?>getScScore(@PathVariable String riderId){
        Result<?> scoreResult = getScScore(riderId);
        return Result.ok(scoreResult);
    }
    /**
     * 积分增加
     */
    @PostMapping("/add")
    public Result<?>addScScore(@PathVariable String riderId,@PathVariable  Integer chanalId){
        //判断乘车人是否存在 不存在则添加
        ScScore isScScore = scScoreService.getOne(new LambdaQueryWrapper<ScScore>().eq(ScScore::getRiderId,riderId));
        //查询渠道信息
        ScChanal scChanal = scChanalService.getOne(new LambdaQueryWrapper<ScChanal>().eq(ScChanal::getId,chanalId));
        if(ObjectUtil.isNull(isScScore)){
            //空、创建乘车人后加积分
            isScScore.setRiderId(riderId).setChanalId(chanalId).setCountScore(isScScore.getCountScore()+scChanal.getStatus());
            scScoreService.save(isScScore);
            return Result.ok(isScScore);
        }else {
            //不为空 加积分
            isScScore.setCountScore(isScScore.getCountScore()+scChanal.getStatus());
            scScoreService.save(isScScore);
        }
        return Result.ok(isScScore);
    }

}
